﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;

namespace lwzlz.Web.Extension.Design
{
    public sealed class Singleton
    {
        private static volatile Singleton uniqueInstance;

        private static readonly object locker = new object();

        private Singleton()
        {
            var cache = HttpContext.Current.Cache;
        }
        public static Singleton GetInstance()
        {
            if (uniqueInstance == null)
            {
                lock (locker)
                {

                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new Singleton();
                    }
                }
            }
            return uniqueInstance;
        }
        public int Number()
        {
            return 1;
        }
        public int Number(int i = 0)
        {
            return i;
        }
    }

    public abstract class TestA
    {
        public string code = "code全局";
        public TestA()
        {
            string code = "code局部";
            string xx = this.code + code;
        }
        public TestA(int i = 0) : this()
        {

        }

        public abstract string Get();

        public virtual string Get1()
        {
            return "1";
        }
    }

    public class Test : TestA
    {
        public Test() : base()
        {

        }
        public Test(int i) : base(i)
        {

        }
        public override string Get()
        {
            return "1111";
        }
    }


    /// <summary>
    /// EPList 支持为List创建索引
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    public class EPList<T>
    {
        #region 成员变量

        /// <summary>
        /// 索引
        /// </summary>
        private List<string[]> m_Index = new List<string[]>();

        /// <summary>
        /// 缓存数据
        /// </summary>
        private Dictionary<string, List<T>> m_CachedData = new Dictionary<string, List<T>>();

        /// <summary>
        /// List数据源
        /// </summary>
        private List<T> m_ListData = new List<T>();

        /// <summary>
        /// 通过索引值取数据
        /// </summary>
        /// <param name="indexFields">索引字段</param>
        /// <param name="fieldValues">字段值</param>
        /// <returns></returns>
        public List<T> this[string[] indexFields]
        {
            get
            {
                string key = string.Join(",", indexFields);
                if (m_CachedData.ContainsKey(key)) return m_CachedData[key];
                return new List<T>();
            }
        }
        #endregion

        #region 公共方法

        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="indexFields">索引字段</param>
        public void CreateIndex(string[] indexFields)
        {
            if (m_Index.Contains(indexFields)) return;
            m_Index.Add(indexFields);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="record">记录</param>
        public void Add(T record)
        {
            m_ListData.Add(record);
            m_Index.ForEach(indexFields =>
            {
                string key = getKey(record, indexFields);
                if (m_CachedData.ContainsKey(key))
                {
                    m_CachedData[key].Add(record);
                }
                else
                {
                    List<T> list = new List<T> { record };
                    m_CachedData.Add(key, list);
                }
            });
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="record">记录</param>
        /// <param name="fieldName">字段名</param>
        /// <returns></returns>
        private object getValue(T record, string fieldName)
        {
            Type type = typeof(T);
            PropertyInfo propertyInfo = type.GetProperty(fieldName);
            return propertyInfo.GetValue(record, null);
        }

        /// <summary>
        /// 获取Key
        /// </summary>
        /// <param name="record">记录</param>
        /// <param name="indexFields">索引字段</param>
        private string getKey(T record, string[] indexFields)
        {
            List<string> values = new List<string>();
            foreach (var field in indexFields)
            {
                string value = Convert.ToString(getValue(record, field));
                values.Add(field + ":" + value);
            }
            return string.Join(",", values);
        }

        /// <summary>
        /// 获取Key
        /// </summary>
        /// <param name="indexFields">索引字段</param>
        /// <param name="fieldValues">字段值</param>
        /// <returns></returns>
        private string getKey(string[] indexFields, object[] fieldValues)
        {
            if (indexFields.Length != fieldValues.Length) return string.Empty;
            for (int i = 0; i < indexFields.Length; i++)
            {
                fieldValues[i] = indexFields[i] + ":" + fieldValues[i];
            }
            string key = string.Join(",", fieldValues);
            return key;
        }

        #endregion
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class StringLengthAttribute : Attribute
    {
        public StringLengthAttribute(string displayName, int maxLength)
        {
            this.DisplayName = displayName;
            this.MinLength = maxLength;
        }

        public string DisplayName { get; set; }

        public int MaxLength { get; set; }

        public string ErrorMessage { get; set; }

        public int MinLength { get; set; }


    }

    [Serializable]
    public class Order
    {
        [StringLength("订单号", maxLength: 6, MinLength = 3, ErrorMessage = "{0}的长度必须在{1}和{2}之间！")]
        public string OrderID { get; set; }
    }

    public class Program
    {
        public static bool isIDLengthValid(int IDLength, MemberInfo member)
        {
            foreach (object attribute in member.GetCustomAttributes(true))
            {


                if (attribute is StringLengthAttribute)
                {
                    StringLengthAttribute attr = (StringLengthAttribute)attribute;
                    if (IDLength < attr.MinLength || IDLength > attr.MaxLength)
                    {
                        string displayName = attr.DisplayName;
                        int maxLength = attr.MaxLength;
                        int minLength = attr.MinLength;
                        string error = attr.ErrorMessage;
                        Console.WriteLine(error, displayName, maxLength, minLength);//验证失败，提示错误
                        return false;
                    }
                    else return true;
                }
            }
            return false;
        }

        public static bool isOrderValid(Order order)
        {

            Type t = typeof(Order);//运算符
            Type t1 = order.GetType();


            bool isOK = typeof(Order).IsDefined(typeof(StringLengthAttribute), false);
            if (isOK == true)
            {
                if (order == null) return false;
                foreach (PropertyInfo p in typeof(Order).GetProperties())
                {
                    if (isIDLengthValid(order.OrderID.Length, p))//1记录下新对象需要验证的数据，

                        return true;
                }
            }

            return false;

        }
    }
}